<script setup lang="ts">
import { reactive, ref, onMounted, computed } from 'vue'
import { Delete, Search, Plus } from '@element-plus/icons-vue'
import { type AmsAssetType } from '@/api/ams'
import { getAssetTypeList, addAssetType, delAssetType } from '@/api/ams'

defineOptions({
  name:"AssetType"
})

const searchDto = reactive({
  name: '',
})

const filterTableData = computed(() =>
  tableData.value.filter(
    (data) => !searchDto.name || data.name.toLowerCase().includes(searchDto.name.toLowerCase()),
  ),
)

const tableData = ref<AmsAssetType[]>([])
const isAddDialog = ref(true) // 是否为添加模式
const showDialog = ref(false) // 是否显示模态框
const dialogForm = reactive<AmsAssetType>({})

const clickAddBtn = () => {
  isAddDialog.value = true
  dialogForm.name = ''
  dialogForm.description = ''
  showDialog.value = true
}

const clickSearchBtn = () => {
  getAssetTypeList().then((res) => {
    tableData.value = res
  })
}
onMounted(() => {
  clickSearchBtn()
})

const clickSubDialogForm = () => {
  addAssetType(dialogForm).then(() => {
    clickSearchBtn()
    showDialog.value = false
  })
}

const clickDelBtn = (id: string) => {
  delAssetType(id).then(() => {
    clickSearchBtn()
  })
}
</script>

<template>
  <div>
    <el-dialog
      v-model="showDialog"
      :title="isAddDialog ? '添加资产类型' : '更新资产类型'"
      width="500"
    >
      <el-form :model="dialogForm" label-width="auto" style="max-width: 600px">
        <el-form-item label="名称">
          <el-input v-model="dialogForm.name" />
        </el-form-item>
        <el-form-item label="描述">
          <el-input
            v-model="dialogForm.description"
            :rows="2"
            type="textarea"
            placeholder="描述信息"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="clickSubDialogForm"> 提交 </el-button>
        </div>
      </template>
    </el-dialog>
    <el-row>
      <el-col>
        <el-card>
          <el-row class="mb-5">
            <el-col>
              <el-input
                v-model="searchDto.name"
                class="max-w-xs mr-5"
                placeholder="请输入类型名称"
                clearable
              />
              <el-button type="primary" :icon="Search" @click="clickSearchBtn">查询</el-button>
              <el-button type="primary" :icon="Plus" @click="clickAddBtn">添加</el-button>
            </el-col>
          </el-row>
          <el-table :data="filterTableData" stripe style="width: 100%" show-overflow-tooltip>
            <el-table-column type="index" width="50" label="#" />
            <el-table-column prop="name" label="名称" width="100" />
            <el-table-column prop="description" label="描述" />
            <el-table-column fixed="right" label="操作" width="130">
              <template #default="scope">
                <el-link
                  :icon="Delete"
                  type="primary"
                  class="mr-3"
                  @click="clickDelBtn(scope.row.id)"
                  >删除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped></style>
